package com.bpzj.drools.config;

import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.Results;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.internal.io.ResourceFactory;
import org.kie.spring.KModuleBeanFactoryPostProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

@Configuration
@Slf4j
public class RuleEngineConfig {


  @Bean
  public KieServices getKieService() {
    KieServices kieServices = KieServices.Factory.get();
    return kieServices;
  }

  @Bean
  public KieRepository getKieRepository() {
    return getKieService().getRepository();
  }

  @Bean
  public KieModuleModel getKieModule() {
    return getKieService().newKieModuleModel();
  }

//  @Bean
//  public KieFileSystem getKieFileSystem() {
//    KieFileSystem kfs = getKieService().newKieFileSystem();
//    return kfs;
//  }

//  @Bean
//  public KieContainer getKieContainer() {
//    KieRepository kieRepository = getKieRepository();
//    kieRepository.addKieModule(getKieModule());
//    return getKieService().newKieContainer(kieRepository.getDefaultReleaseId());
//  }


  @Bean
  @ConditionalOnClass({KieFileSystem.class,KieModuleModel.class})
  public KieContainer getKieContainer() throws IOException {
//    KieRepository kieRepository = getKieService().getRepository();
//    KieBuilder kieBuilder = getKieService().newKieBuilder(kieFileSystem()).buildAll();
//    Results results = kieBuilder.getResults();
//    KieFileSystem kfs = getKieService().newKieFileSystem();
//    KieModuleModel kieModuleModel = getKieModule();
//    kfs.writeKModuleXML(kieModuleModel.toXML());
//    KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("kBaseName");
    KieServices kieService = getKieService();
    KieRepository kieRepository = getKieRepository();
    kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
    KieBuilder kieBuilder = kieService.newKieBuilder(kieFileSystem());
    kieBuilder.buildAll();
    return kieService.newKieContainer(kieRepository.getDefaultReleaseId());
  }

  @Bean
  public KieFileSystem kieFileSystem() throws IOException {
    KieFileSystem kieFileSystem = getKieService().newKieFileSystem();
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
//    在 springboot 项目中, 不要使用 org.springframework.util.ResourceUtils.getFile()
//    也就是 ResourcePatternResolver.getResources() 方法 todo
//   推荐使用 ClassPathResource
    ClassPathResource resource = new ClassPathResource("rules/");
    Resource[] files = resourcePatternResolver.getResources("classpath*:rules/*.*");
    String path = null;
//    KieModuleModel kieModuleModel = getKieService().newKieModuleModel();
    KieModuleModel kieModuleModel = getKieModule();
    // 在for循环中, 更新 kieModuleModel
    for (Resource file : files) {
      String kBaseName = "rules.test";
      path = "rules/" + file.getFilename();
      log.info("path=" + path);
      kieFileSystem.write(ResourceFactory.newClassPathResource(path, "UTF-8"));
      //  addPackage 方法的参数, 添加的是 drl 文件中定义的 package
      KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel(kBaseName).addPackage("rules");
      kieBaseModel.newKieSessionModel("session_" + kBaseName);
    }

    kieFileSystem.writeKModuleXML(kieModuleModel.toXML());
    return kieFileSystem;
  }



//    @Bean
//    public KieBase kieBase() throws IOException {
//        return kieContainer().getKieBase();
//    }

//    @Bean
//    public KieSession kieSession() throws IOException {
//        return kieContainer().newKieSession();
//    }

  @Bean
  public KModuleBeanFactoryPostProcessor kiePostProcessor() {
    return new KModuleBeanFactoryPostProcessor();
  }

}
